<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['diy_option']=array(
	'0'=>'默认',
	'1'=>'自定义'
);
if($_RQ['op']=='main'){	
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要设计的网站！','error');
	}
	$params=array('id'=>$id);
	if($_RQ['system'] && $_SESSION['user']['type']>3){
		$params['uniacid']=0;
	}else{
		$params['uniacid']=$_SESSION['uniacid'];
	}
	$item=pdo_get('core_mobile_web',$params,array('id','uniacid','type','pctype','pc_items','head_id','foot_id','title','is_narrow'));
	if(!$item){
		exi('指定网站不存在！','error');
	}
	if($_RQ['system'] && $_SESSION['user']['type']>3){
		$item['system']=1;
		$return['site']=core_setting('site');
		$operating=core_setting('operating');
		$return['operating']=$operating;
	}
	$item['pc_items']=$item['pc_items']?json_decode($item['pc_items'],true):array();
	if($item['pctype']<2){
		//普通页面，加载头部底部
		if($item['head_id']){
			$item['head_items']=pdo_getcolumn('core_mobile_web',array('id'=>$item['head_id']),'pc_items');
		}
		$item['head_items']=$item['head_items']?json_decode($item['head_items'],true):array();
		if($item['foot_id']){
			$item['foot_items']=pdo_getcolumn('core_mobile_web',array('id'=>$item['foot_id']),'pc_items');
		}
		$item['foot_items']=$item['foot_items']?json_decode($item['foot_items'],true):array();
	}
	if($item['type']==2){
		$item['head_items']=$item['pc_items'];
	}
	if($item['type']==3){
		$item['foot_items']=$item['pc_items'];
	}
	$return['item']=$item;
	$return['list_r']=pdo_getall('core_pc_blocks',array('type'=>1,'cloud_id >'=>0),array('cloud_id','title','default','type','image'));
	if($return['list_r']){
		foreach($return['list_r'] as &$row){
			$row['default']=json_decode($row['default'],true);
		}
	}
	$systype=$item['uniacid']?0:1;
	$return['list_h']=pdo_getall('core_pc_blocks',array('type'=>2,'cloud_id >'=>0,'systype'=>$systype),array('cloud_id','title','default','type','image'));
	if($return['list_h']){
		foreach($return['list_h'] as &$row){
			$row['default']=json_decode($row['default'],true);
		}
	}
	$return['list_f']=pdo_getall('core_pc_blocks',array('type'=>3,'cloud_id >'=>0,'systype'=>$systype),array('cloud_id','title','default','type','image'));
	if($return['list_f']){
		foreach($return['list_f'] as &$row){
			$row['default']=json_decode($row['default'],true);
		}
	}
	$return['list_c']=pdo_getall('core_pc_blocks',array('type'=>4,'cloud_id >'=>0),array('cloud_id','title','default','type','image'));
	if($return['list_c']){
		foreach($return['list_c'] as &$row){
			$row['default']=json_decode($row['default'],true);
		}
	}
	$return['blocks']=pdo_getall('core_pc_blocks',array('cloud_id >'=>0),array('cloud_id','edithtml','showhtml'),'cloud_id');
	foreach($return['blocks'] as &$block){
		$block['showhtml']=cfc('files')->source_read($block['showhtml']);
	}
	$return['token']=$_SESSION['token'];
	$return['extradata']=array();
	set_html('header','common/header-cms');
	set_html('title',$item['title'].'设计');
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('请先选择要设计的网站！','error');
	}
	$params=array('id'=>$id);
	if($post['system'] && $_SESSION['user']['type']>3){
		$params['uniacid']=0;
	}else{
		$params['uniacid']=$_SESSION['uniacid'];
	}
	$design=pdo_get('core_mobile_web',$params);
	if(!$design){
		exi('指定网站不存在！','error');
	}
	if($post['type']<2 && !$post['pc_items']){
		exi('请先设计好<页面主内容>再提交','error');
	}
	if($post['type']==2 && !$post['head_items']){
		exi('请先设计好<页面头部>再提交','error');
	}
	if($post['type']==3 && !$post['foot_items']){
		exi('请先设计好<底部头部>再提交','error');
	}
	$params=array('pc_items'=>$post['pc_items']);
	//$cs=ijson_encode($post['head_items']);echo $cs;exit('xx');
	$update=array('pc_items'=>ijson_encode($post['pc_items']),'is_narrow'=>$post['is_narrow']);
	if($post['head_items']){
		$params['head_items']=$post['head_items'];
	}
	if($post['foot_items']){
		$params['foot_items']=$post['foot_items'];
	}
	$local_madehtml=pdo_getall('core_pc_blocks',array('madehtml <>'=>''),array('cloud_id','default','madehtml'),'cloud_id');
	if($local_madehtml){
		foreach($local_madehtml as &$row){
			$row['madehtml']=cfc('files')->source_read($row['madehtml']);
			$row['default']=$row['default']?json_decode($row['default'],true):array();
		}
	}
	$params['local_madehtml']=$local_madehtml;
	$res=cloud_request('diy.pc.design',$params);
	if($res['pc_html']){
		$update['pc_html']=cfc('files')->source_save($res['pc_html'],'page','html',$design['pc_html']);
	}
	if($res['head_html']){
		if($post['head_id']){
			$old=pdo_get('core_mobile_web',array('id'=>$post['head_id']),array('id','head_html'));
		}
		if($post['type']==2){
			$temp=cfc('files')->source_save($res['head_html'],'page','html',$design['head_html']);
		}else{
			$temp=cfc('files')->source_save($res['head_html'],'page','html',$old['head_html']);
		}
		if($post['type']==2){
			$update['pc_items']=ijson_encode($post['head_items']);
			$update['head_html']=$temp;
			//pdo_update('core_mobile_web',array('pc_items'=>ijson_encode($post['head_items']),'head_html'=>$temp),array('id'=>$id));
		}elseif($post['head_id'] && $old['id']){
			pdo_update('core_mobile_web',array('pc_items'=>ijson_encode($post['head_items']),'head_html'=>$temp),array('id'=>intval($post['head_id'])));
		}else{
			$insert=array(
				'uniacid'=>$design['uniacid'],
				'pctype'=>2,
				'status'=>1,
				'title'=>$post['title'].'-TOP顶部模板',
				'pc_items'=>ijson_encode($post['head_items']),
				'head_html'=>$temp,
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('core_mobile_web',$insert);
			$update['head_id']=pdo_insertid();
		}
	}
	if($res['foot_html']){
		if($post['foot_id']){
			$old=pdo_get('core_mobile_web',array('id'=>$post['foot_id']),array('id','foot_html'));
		}
		if($post['type']==3){
			$temp=cfc('files')->source_save($res['foot_html'],'page','html',$design['foot_html']);
		}else{
			$temp=cfc('files')->source_save($res['foot_html'],'page','html',$old['foot_html']);
		}
		if($post['type']==3){
			$update['pc_items']=ijson_encode($post['foot_items']);
			$update['head_html']=$temp;
			//pdo_update('core_mobile_web',array('pc_items'=>ijson_encode($post['foot_items']),'foot_html'=>$temp),array('id'=>$id));
		}elseif($post['foot_id'] && $old['id']){
			pdo_update('core_mobile_web',array('pc_items'=>ijson_encode($post['foot_items']),'foot_html'=>$temp),array('id'=>intval($post['foot_id'])));
		}else{
			$insert=array(
				'uniacid'=>$design['uniacid'],
				'pctype'=>3,
				'status'=>1,
				'title'=>$post['title'].'-FOOT底部模板',
				'pc_items'=>ijson_encode($post['foot_items']),
				'foot_html'=>$temp,
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('core_mobile_web',$insert);
			$update['foot_id']=pdo_insertid();
		}
	}
	$update['updatetime']=TIMESTAMP;
	pdo_update('core_mobile_web',$update,array('id'=>$id));
	$look=intval($_RQ['look']);
	if($look){
		if($design['uniacid']==0){
			$url=manage_url('/home.diy//id='.$id,true);
		}else{
			$url=custom_url('/welcome//id='.$id,true);
		}
		exi('保存成功！点击确认进入预览界面！','','',$url);
	}else{
		exi('保存成功！','','hold');
	}
}
if($_RQ['op']=='html'){
	//获取组件html
	$id=intval($_RQ['id']);
	$item=trim($_RQ['item']);
	$html=pdo_getcolumn('core_pc_blocks',array('cloud_id'=>$id),'showhtml');
	$html=cfc('files')->source_read($html);
	$html=str_replace('{item}',$item,$html);
	exit($html);
}

